package com.zrrd.yunchmall.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.zrrd.yunchmall.product.entity.*;
import com.zrrd.yunchmall.product.mapper.ProductMapper;
import com.zrrd.yunchmall.product.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息 服务实现类
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@SuppressWarnings("all")
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
    @Autowired
    private IProductAttributeValueService iProductAttributeValueService;

    @Autowired
    private IMemberPriceService iMemberPriceService;

    @Autowired
    private IProductLadderService iProductLadderService;

    @Autowired
    private IProductFullReductionService iProductFullReductionService;

    @Autowired
    private ISkuStockService iSkuStockService;

    /**
     * 重写Mybatis-plus中的save方法
     */
    @Override
    @Transactional
    public boolean save(Product product) {
        // 1.保存商品信息
        super.save(product);
        // 2.保存商品属性值信息
        List<ProductAttributeValue> productAttributeValueList = product.getProductAttributeValueList();
        productAttributeValueList.forEach(item -> {
            item.setProductId(product.getId());// 设置商品id
        });

        iProductAttributeValueService.saveBatch(productAttributeValueList);// 将所有项批量添加
        // 3.保存商品会员价格信息
        List<MemberPrice> memberPriceList = product.getMemberPriceList();
        memberPriceList.forEach(item -> {
            item.setProductId(product.getId());// 设置商品id
        });
        iMemberPriceService.saveBatch(memberPriceList);
        // 4.保存折扣信息
        List<ProductLadder> productLadderList = product.getProductLadderList();
        productLadderList.forEach(item -> {
            item.setProductId(product.getId());// 设置商品id
        });
        iProductLadderService.saveBatch(productLadderList);
        // 5.商品满减信息
        List<ProductFullReduction> productFullReductionList = product.getProductFullReductionList();
        productFullReductionList.forEach(item -> {
            item.setProductId(product.getId());// 设置商品id
        });
        iProductFullReductionService.saveBatch(productFullReductionList);
        // 6.SKU库存信息
        List<SkuStock> skuStockList = product.getSkuStockList();
        String skuCodePrefix = System.currentTimeMillis() + "";
        for (int i = 0; i < skuStockList.size(); i++) {
            skuStockList.get(i).setProductId(product.getId());
            String x = i < 10 ? ("0" + i) : (i + "");
            skuStockList.get(i).setSkuCode(skuCodePrefix + x);
        }
        iSkuStockService.saveBatch(skuStockList);
        // 7.操作日志
        ProductOperateLog operateLog = new ProductOperateLog();
        return true;
    }
    @Override
    @Transactional
    public void freeStock(List<Map<String, Long>> params) {
        // 遍历Params
        params.forEach(item -> {
            long productId = item.get("productId");
            long skuId = item.get("skuId");
            long quantity = item.get("quantity");
            UpdateWrapper updateWrapper = new UpdateWrapper<>();
            QueryWrapper queryWrapper = new QueryWrapper<>();
            queryWrapper.select("stock");
            queryWrapper.eq("id", productId);
            int stock = super.getOne(queryWrapper).getStock();
            updateWrapper.set("stock", stock + quantity);
            updateWrapper.eq("id", productId);
            // 更新商品库存
            super.update(updateWrapper);
            updateWrapper.clear();
            queryWrapper.clear();
            queryWrapper.select("stock");
            queryWrapper.eq("id", skuId);
            stock = iSkuStockService.getOne(queryWrapper).getStock();
            updateWrapper.set("stock", stock + quantity);
            updateWrapper.eq("id", skuId);
            // 更新sku库存
            iSkuStockService.update(updateWrapper);
        });
    }
}
